'''
过滤文件 将位姿标定错误的文件剔除
'''

import os
import open3d as o3d
import numpy as np
import random
import json

import sys
import shutil
from tqdm import tqdm

def get_files(folder,filename):

    path = os.path.join(folder, filename)
    dir = os.path.dirname(path)
    if not os.path.exists(dir):
        os.makedirs(dir)
    return path

def files_id(filenames):
    return filenames.split('.')[0]

def files_ids(filenames):
    return f'{int(filenames.split(".")[0])}.jpg'

def files_ids_png(filenames):
    return f'{int(filenames.split(".")[0])}.png'

def png2jpg(filename):
    return filename.split('.')[0] + '.jpg'

'''

对原始的数据集进行筛选，提取出标定正确的数据集

'''
folder ="E:/pose/datasets/obj_ac/obj_000003/obj_000003"
to_folder = "E:/pose/datasets/obj_ac/003"

def filter_bad_pose(folder,to_folder):
    length = len(os.listdir(os.path.join(folder,'results')))
    for idx, filename in enumerate(tqdm(os.listdir(os.path.join(folder,'results')))):
        shutil.copyfile(get_files(folder,f"JPEGImages/{files_ids(filename)}"),get_files(to_folder,f"rgb/{idx}.jpg"))
        # shutil.copyfile(get_files(folder,f"depth/{files_ids_png(filename)}"),get_files(to_folder,f"depth/{idx}.jpg"))
        shutil.copyfile(get_files(folder,f"poses-martix/{files_id(filename)}.txt"),get_files(to_folder,f"poses/{idx}.txt"))

    return length

filter_bad_pose(folder,to_folder)



